
console.log( 'load main ...')
var app = angular.module( 'app',['ui.router'] ) ;
app.directive('script' , function(){
	var option = {
		restrict:  'E' ,
		link 	:  function( scope , ele , attr ){
			var sc = document.createElement('script') ;
			if( !attr.src ){
				sc.innerHTML = ele.text() ;
			}else{
				sc.src = attr.src ;
			}
			document.body.appendChild( sc ) ;
			ele.remove();
		} 	
	};
	return option ;
});
app.config(function( $stateProvider , $urlRouterProvider , $controllerProvider ){
	console.log( $stateProvider , $urlRouterProvider )
	$urlRouterProvider.when('','/page1') ;

	$stateProvider.state('page1' , {
		url 	: 	'/page1' ,
		template: 	'<div>page1</div>' ,
		name 	:  	'main'
	})
	.state('page2',{
		url 	: 	'/page2' ,
		views	: 	{
			side : {
				templateProvider : function(){
					return 'page2@side' ;
				}
			},
			'' 	: {
				template : 'page2'
			}
		}
	})
	.state('/page3/?id&name',{
		url 	: 	'/page3' ,
		templateUrl:function(){
			console.log('a')
			return './page3.html' ;
		},
		controller:'module1Controller' ,
		resolve: {
                        /*
                        这个key值会被注入到controller中，对应的是后边这个function返回的值，或者promise最终resolve的值。函数的参数是所需的服务，angular会根据参数名自动注入
                         对应controller写法（注意keyName）：
                         controllers.controller('module2Controller', ['$scope', '$http', 'keyName',
                             function($scope, $http, keyName) {
                         }]);
                         */
                        keyName: function ($q) {
                            var deferred = $q.defer();
                            require(['page3.js'], function (controller) {
                                $controllerProvider.register('module1Controller', controller);      //由于是动态加载的controller，所以要先注册，再使用
                                deferred.resolve();
                            });
                            return deferred.promise;
                        }
                 }
	})
});
app.constant('appName','v.global.1.0');
app.controller('demoCtrl',['$scope' , 'facService' , 'appName' , function($scope ,facService, appName ){
	console.log( 'appName:'+appName ) ;
	appName = 'abc' ;
	console.log( 'appName:'+appName ) ;
	facService.test() ;
	$scope.clickTest = function(){
		console.log( 'click...' ) ;
	}
}]);

app.factory('facService',function(){
	var service = {} ;
	service.test = function(){
		console.log('factory ...') ;
	}
	this.test = function(){
		console.log( 'service ...' ) ;
	}
	this.$get = function(){
		return {
			test : function(){
				console.log('provider ...') ;
			}
		}
	}
	return service ;
});


/*resolve: {
    load: ['$q', function($q) {
        var defer = $q.defer();
        require(['/test/lazyspa/module1.js'], function(loader) {
            loader.onload && loader.onload(function() {
                defer.resolve();
            });
        });
        return defer.promise;
    }]
}*/
